package com.qf.controller.business;

import cn.hutool.captcha.CaptchaUtil;
import cn.hutool.captcha.ShearCaptcha;
import com.qf.entitys.Business;
import com.qf.entitys.RespBean;
import com.qf.entitys.TbManagers;
import com.qf.service.BusinessService;
import com.qf.service.TbManagersService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

@Controller
@RequestMapping("/business")
public class BLoginController {

    @Resource
    private BusinessService businessService;
    //跳转login
    @GetMapping("/login")
    public String login(){
        return "business/login";
    }

    //检查登录
    @PostMapping("/logincheck")
    @ResponseBody
    public RespBean logincheck(Business business, HttpSession session){
        Business b = businessService.selectByIdAndPwd(business);
        if (b!=null){
            session.setAttribute("login",b);
            return RespBean.success("正确");
        }else {
            return RespBean.error("错误");
        }
    }

    //Hutool图形验证码
    @GetMapping("/valicode")
    public void valicode(HttpSession session, HttpServletResponse response) throws IOException {
        //定义图形验证码的长、宽、验证码字符数、干扰线宽度
        ShearCaptcha captcha = CaptchaUtil.createShearCaptcha(200, 100, 4, 4);
        session.setAttribute("code",captcha);
        //将验证码给到请求的地方
        captcha.write(response.getOutputStream());
    }

    //图像验证码验证
    @PostMapping("/vlicodecheck")
    @ResponseBody
    public RespBean valicodecheck(HttpSession session, HttpServletResponse response) throws IOException {
        ShearCaptcha code = (ShearCaptcha)session.getAttribute("code");
//        PrintWriter out = response.getWriter();
//        out.println(code.getCode());
        return RespBean.success(200,"请求成功",code.getCode());
    }

}
